using LiJ.Common.Core;
using LiJ.Http.Extention;
using LiJ.Http.Mos;
using LiJ.Framework.Official.Basic.Mos;
using System.Threading.Tasks;

namespace LiJ.Framework.Official.SmallApp
{
	/// <summary>
	///  微信小程序相关接口
	/// </summary>
	public class WxOffAppApi : WxOffBaseApi
	{
		/// <summary>
		/// 小程序接口
		/// </summary>
		/// <param name="config">配置信息，如果这里不传，需要在程序入口静态 WxBaseApi.DefaultConfig 属性赋值</param>
		public WxOffAppApi(AppConfig config = null)
			: base(config)
		{
		}

		/// <summary>
		///  获取小程序二维码
		/// 生成二维码的总数不限，但接口调用每天上限 10000 次
		/// </summary>
		/// <param name="path"></param>
		/// <param name="width"></param>
		/// <returns></returns>
		public async Task<WxFileResp> DownloadMediaAsync(string path, int width)
		{
			WxOffAccessTokenResp wxOffAccessTokenResp = await this.GetAccessTokenFromCacheAsync();
			if (!wxOffAccessTokenResp.IsSuccess())
			{
				return ((ResultMo)wxOffAccessTokenResp).ConvertToResult<WxFileResp>();
			}
			return await new LHttpRequest
			{
				HttpMothed = HttpMothed.Post,
				AddressUrl = "https://api.weixin.qq.com" + "/cgi-bin/wxaapp/createwxaqrcode?access_token=" + wxOffAccessTokenResp.access_token,
				CustomBody = string.Format("{{\"path\":\"{0}\",\"width\":{1}}}", path, width)
			}.RestCommon(WxOffBaseApi.DownLoadFileAsync, null, "default");
		}
	}
}
